package com.zhaolixiang.view.animation12

import android.animation.Keyframe
import android.animation.LayoutTransition
import android.animation.ObjectAnimator
import android.animation.PropertyValuesHolder
import android.app.Activity
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    private var i=0;
    private var j=0;
    private lateinit var activity:Activity
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        activity=this
        bt_add.setOnClickListener{
            add()
        }
        bt_delete.setOnClickListener{
            delete()
        }

        val layoutTransition=LayoutTransition()
        //入场动画
        val objectAnimator=ObjectAnimator.ofFloat(null,"rotation",0f,90f,0f)
        layoutTransition.setAnimator(LayoutTransition.APPEARING,objectAnimator)

        //出厂动画
        val objectAnimator2=ObjectAnimator.ofFloat(null,"rotationY",0f,-90f,0f)
        layoutTransition.setAnimator(LayoutTransition.DISAPPEARING,objectAnimator2)


        //CHANGE_APPEARING
        val leftPropertyValueHolder=PropertyValuesHolder.ofInt("left",0,100,0)
        val rightPropertyValueHolder=PropertyValuesHolder.ofInt("top",1,1)
        val scalePropertyValueHolder=PropertyValuesHolder.ofFloat("ScaleX",1f,9f,1f)
        val objectAnimator3=ObjectAnimator.ofPropertyValuesHolder(ll_add2,leftPropertyValueHolder,rightPropertyValueHolder,scalePropertyValueHolder)
        layoutTransition.setAnimator(LayoutTransition.CHANGE_APPEARING,objectAnimator3)

        //CHANGE_DISAPPEARING
        val leftPropertyValueHolder4=PropertyValuesHolder.ofInt("left",0,0)
        val rightPropertyValueHolder4=PropertyValuesHolder.ofInt("top",1,1)
        val keyFrame0=Keyframe.ofFloat(0f,0f)
        val keyFrame1=Keyframe.ofFloat(0.1f,-20f)
        val keyFrame2=Keyframe.ofFloat(0.2f,20f)
        val keyFrame3=Keyframe.ofFloat(0.3f,-20f)
        val keyFrame4=Keyframe.ofFloat(0.4f,20f)
        val keyFrame5=Keyframe.ofFloat(0.5f,-20f)
        val keyFrame6=Keyframe.ofFloat(0.6f,20f)
        val keyFrame7=Keyframe.ofFloat(0.7f,-20f)
        val keyFrame8=Keyframe.ofFloat(0.8f,20f)
        val keyFrame9=Keyframe.ofFloat(0.9f,-20f)
        val keyFrame10=Keyframe.ofFloat(1.0f,0f)
        val keyFramePropertyValue=PropertyValuesHolder.ofKeyframe("rotation",
                keyFrame0,
                keyFrame1,
                keyFrame2,
                keyFrame3,
                keyFrame4,
                keyFrame5,
                keyFrame6,
                keyFrame7,
                keyFrame8,
                keyFrame9,
                keyFrame10)
        val objectAnimator4=ObjectAnimator.ofPropertyValuesHolder(ll_add2,leftPropertyValueHolder4,rightPropertyValueHolder4,
                keyFramePropertyValue)
        layoutTransition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING,objectAnimator4)

        //添加监听
        layoutTransition.addTransitionListener(object:LayoutTransition.TransitionListener{
            override fun startTransition(transition: LayoutTransition?, container: ViewGroup?, view: View?, transitionType: Int) {
                Log.e("开始了","startTransition")
            }

            override fun endTransition(transition: LayoutTransition?, container: ViewGroup?, view: View?, transitionType: Int) {
                Log.e("结束了","endTransition")
            }

        })


        ll_add2.layoutTransition=layoutTransition
        bt_layout_transaction_add.setOnClickListener{
            layoutTransitionAdd()
        }
        bt_layout_transaction_delete.setOnClickListener{
            layoutTransitionDelete()
        }



    }
    fun add(){
        i++
        val button= Button(activity)
        button.text="Button"+i
        val param=LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT)
        button.layoutParams=param
        ll_add.addView(button,0)
    }
    fun delete(){
        if(i>0){
            ll_add.removeViewAt(0)
        }
        i--
    }

    private fun layoutTransitionAdd(){
        j++
        val button= Button(activity)
        button.text="Button"+j
        val param=LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT)
        button.layoutParams=param
        ll_add2.addView(button,0)
    }
    private fun layoutTransitionDelete(){
        if(j>0){
            ll_add2.removeViewAt(0)
        }
        j--
    }
}
